package com.daybook.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.daybook.pojo.Books;
import com.daybook.pojo.Category;
import com.daybook.pojo.Order;
import com.daybook.pojo.OrderAndBooks;
import com.daybook.pojo.User;
import com.daybook.service.OrderService;
import com.daybook.util.PageHelp;

@Controller
@RequestMapping(value = "/orderMsg",method = RequestMethod.POST)
public class OrderController {
	 @Autowired
	 private OrderService orderService;
		//1.购物车结算,添加订单
		@RequestMapping(value = "/paymentCart", method = RequestMethod.POST)
		public void paymentCart(HttpServletRequest request, HttpServletResponse response,
				Integer[] sid_list,
				Integer[] bid_list,
				Double[] now_price_list,
				Double[] pay_price_list,Integer[] pay_number_list,
				String utel,String uaddress,String payMoney
				)
				throws IOException, JSONException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			HttpSession session=request.getSession();
			int result=-1;
			User user=(User)session.getAttribute("user");
			List<Order> orderList=new ArrayList<>();
			//实付金额
			double totalMoney=Double.valueOf(payMoney);
			if(user==null){
				result=0;
			}else{
				//订单时间
				Timestamp paytime=new Timestamp(System.currentTimeMillis());  
				 DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
				  String tsStr = sdf.format(paytime);
				  //订单编码
				  String num="ddb"+tsStr+user.getUid();
				for(int i=0;i<bid_list.length;i++){
					Order order=new Order();
					order.setOnum(num);
					order.setBid(bid_list[i]);
					order.setBprice(now_price_list[i]);
					order.setOprice(pay_price_list[i]);
					order.setOnumber(pay_number_list[i]);
					order.setOtel(utel);
					order.setOaddress(uaddress);
					order.setOtime(paytime);
					order.setOstate(1);
					order.setUid(user.getUid());
					orderList.add(order);
					
				}
			result=orderService.addNewOrderList(orderList,sid_list,totalMoney);	
			}
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("res", result);
			out.print(obj);
			out.close();
		}
		
		//2.用户订单信息
		@RequestMapping(value = "/orderList", method = RequestMethod.POST)
		public void orderList(HttpServletRequest request, HttpServletResponse response)
				throws IOException, JSONException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			// 获得页码
			String num = request.getParameter("num");
			Integer pageNow = Integer.valueOf(num);
			//获取订单状态信息
			String state = request.getParameter("ostate");
			Integer ostate=null;
			if(state!=null){
				 ostate = Integer.valueOf(state);
			}
			//获取订单分页条数信息
			String pnum = request.getParameter("pageNum");
			Integer pageNum=null;
			if(pnum!=null){
				 pageNum = Integer.valueOf(pnum);
			}
			HttpSession session=request.getSession();
			User user=(User)session.getAttribute("user");
			Integer uid=null;
			if(user!=null){
				uid=Integer.valueOf(user.getUid());
			}
			PageHelp page = orderService.getOrderPageList(pageNow, uid, ostate, pageNum);
			// 获取总页数
			int totlpage = page.getTotalPageCount();
			// 强制类型转换
			List<OrderAndBooks> orderList = (List<OrderAndBooks>) page.getList();
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("res", 1);
			obj.put("totalPage", totlpage);
			//查询出来数量
			obj.put("num",orderList.size());
			obj.put("list", orderList);
			out.print(obj);
			out.close();
		}
		
		//3.用户订单状态改变
		@RequestMapping(value = "/changeOrderStatus", method = RequestMethod.POST)
		public void changeOrderStatus(HttpServletRequest request, HttpServletResponse response)
				throws IOException, JSONException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			// 获得页码
			String id = request.getParameter("oid");
			int oid = Integer.valueOf(id);
			//获取订单状态信息
			String state = request.getParameter("ostate");
			int ostate=1;
			if(state!=null){
				 ostate = Integer.valueOf(state);
			}
			Order order=new Order();
			order.setOid(oid);
			order.setOstate(ostate);
			int result=-1;
			result=orderService.changeOrderState(order);
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("res", 1);
		
			out.print(obj);
			out.close();
		}
		
		/*******************管理员模块****************************/
	@RequestMapping(value = "/getAllOrderListByCondition", method = RequestMethod.POST)
	public void getAllOrderListByCondition(HttpServletRequest request, HttpServletResponse response)
				throws IOException, JSONException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			// 获得页码
			String num = request.getParameter("num");
			Integer pageNow=null;
			if(num!=null&&!num.equals("")){
			 pageNow = Integer.valueOf(num);
			}
			//多种条件查询
			String bkey=request.getParameter("bkey");
			String ukey=request.getParameter("ukey");
			String state=request.getParameter("ostate");
			String o_num=request.getParameter("onum");
			Map<String, Object> map=new HashMap<>();
			if(bkey!=null&&!bkey.equals("")){
				map.put("bkey", bkey);
			}
			if(ukey!=null&&!ukey.equals("")){
				map.put("ukey", ukey);
			}
			if(state!=null&&!state.equals("-1")){
				int ostate=Integer.valueOf(state);
				map.put("ostate", ostate);
			}
			if(o_num!=null&&!o_num.equals("")){
					map.put("onum", o_num);
			}
			PageHelp page =orderService.getAllOrderList(map, pageNow);
			// 获取总页数
			int totlpage = page.getTotalPageCount();
			// 强制类型转换
			List<OrderAndBooks> ordersList= (List<OrderAndBooks>) page.getList();
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("res", 1);
			obj.put("totalPage", totlpage);
			obj.put("num", ordersList.size());
			obj.put("list", ordersList);
			out.print(obj);
		}		
		
}
